//package com.fjl;
//
//import co.elastic.clients.elasticsearch.ElasticsearchClient;
//import co.elastic.clients.elasticsearch._types.mapping.Property;
//import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
//import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
//import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
//import org.apache.tika.Tika;
//import org.apache.tika.exception.TikaException;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
//@Service
//public class ElasticsearchIndexService {
//
//    private final ElasticsearchClient elasticsearchClient;
//    private final Tika tika; // 用于解析文档内容
//
//    @Value("${elasticsearch.index-name:documents}")
//    private String indexName; // 从配置文件中获取索引名称
//
//    public ElasticsearchIndexService(ElasticsearchClient elasticsearchClient) {
//        this.elasticsearchClient = elasticsearchClient;
//        this.tika = new Tika(); // 初始化 Tika
//    }
//
//    // 创建 Elasticsearch 索引
//    public void createIndex() throws IOException {
//        // 创建索引请求
//        Map<String, Property> properties = new HashMap<>();
//        properties.put("fileName", new Property.Builder().text(new TextProperty.Builder().build()).build());
//        properties.put("filePath", new Property.Builder().text(new TextProperty.Builder().build()).build());
//        properties.put("content", new Property.Builder().text(new TextProperty.Builder().build()).build());
//
//        CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
//                .index(indexName)
//                .mappings(m -> m.properties(properties))
//                .build();
//
//        // 执行索引创建请求
//        CreateIndexResponse createIndexResponse = elasticsearchClient.indices().create(createIndexRequest);
//        if (createIndexResponse.acknowledged()) {
//            System.out.println("Index created successfully.");
//        } else {
//            System.out.println("Index creation failed.");
//        }
//    }
//
//    // 解析文件并将内容存储到 Elasticsearch
//    public void indexDocument(File file) throws IOException, TikaException {
//        // 使用 Tika 解析文件内容
//        String fileName = file.getName();
//        String filePath = file.getAbsolutePath();
//        String content = tika.parseToString(file);
//
//        // 构建文档对象
//        Map<String, Object> document = new HashMap<>();
//        document.put("fileName", fileName);
//        document.put("filePath", filePath);
//        document.put("content", content);
//
//        // 将文档添加到 Elasticsearch
//        elasticsearchClient.index(index -> index
//                .index(indexName)
//                .id(fileName) // 使用文件名作为文档的唯一标识
//                .document(document)
//        );
//        System.out.println("Document indexed successfully: " + fileName);
//    }
//}
